//
//  Menu.m
//  Chokobo
//
//  Created by Luis Monteros on 8/17/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Menu.h"

#define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);


@implementation Menu

- (void)showMainMenu;
{
	
	entries = [[NSMutableArray alloc] init];
	gates = [[NSMutableArray alloc] init];
	inputs = [[NSMutableArray alloc] init];
	
	debug(@"Welcome to Chokobo!! \nLoading... "
		  "You are now allowed to use the program \n"
		  "Please select an action: \n");
	
	//Main menu loop
	do {
		
		debug(@"Entries: \n"
			  "-------------");
		for(Entry *entry in entries) {		
			debug([NSString stringWithFormat:@"%@", entry.name], nil);
		}		
		debug(@"-------------");
		
		debug(@"Gates \n"
				"----------");
		for(Gate *gate in gates) {
			debug([NSString stringWithFormat:@"%@,%@->%@", [[gate.inputs objectAtIndex:0] name], [[gate.inputs objectAtIndex:1] name], gate.name], nil);
		}
		
		debug(@"----------------------------------\n"
			  "          M  E  N  U: \n"
			  "----------------------------------\n"
			  "1. Add an entry \n"
			  "2. Add a logic gate \n"
			  "3. Analyze paths \n"
			  "0. Exit");
		
		scanf("%i", &mainMenuOption);
		
		
		
		switch (mainMenuOption) {
				
			case 0:
				debug(@"Quitting...");
				
				break;
				
			case 1:
				
				tempEntry = [[Entry alloc] init];
				//[entries addObject: [[Entry alloc] init]];				
				[entries addObject: tempEntry];
				[inputs addObject: tempEntry];
				
				
				break;
				
			case 2:
				[self showGateMenu];
				
			case 3:
				
				break;
				
			default:
				debug(@"Wrong option.");
																
				break;
		}
	} while(mainMenuOption != 0) ;
	
}

- (void) showGateMenu {
	
	int gateMenuOption;
	Gate *createdGate;
	
	debug(@"Select the logic gate that you desire: \n"
		  "1. OR \n"
		  "2. AND \n"
		  "3. NAND \n"
		  "4. NOR \n"
		  "5. XOR");
	
	scanf("%i", &gateMenuOption);
	
	switch (gateMenuOption) {
		case 1:
			debug(@"You picked an OR gate");
			createdGate = [self specifyDualInputGate:gateMenuOption];
			[gates addObject:createdGate];
			[inputs addObject:createdGate];
			
			break;
		case 2:
			debug(@"You picked an AND gate");
			[self specifyDualInputGate:gateMenuOption];			
			break;
		case 3:
			debug(@"You picked a NAND gate");
			[self specifyDualInputGate:gateMenuOption];			
			break;
		case 4:
			debug(@"You picked a NOR gate");
			[self specifyDualInputGate:gateMenuOption];			
			break;
		case 5:
			debug(@"You picked a XOR gate");
			[self specifyDualInputGate:gateMenuOption];			
			break;
		default:
			break;
	}		
}

- (Gate *)specifyDualInputGate: (int)gateType {
	
	int firstInputOption, secondInputOption;
	Gate *newGate = [Gate alloc];
	Entry *entry1, *entry2;
	

	
	//Display the list of all avaiable outputs of each input
	for(int i = 0; i < [inputs count]; i++) {
		debug(@"%d. %@", i, [[inputs objectAtIndex:i] name]);
	}
	debug(@"Choose the first input: ");
	scanf("%i", &firstInputOption);
	debug(@"Choose the second input: ");
	scanf("%i", &secondInputOption);
	
	entry1 = [inputs objectAtIndex:firstInputOption];
	entry2 = [inputs objectAtIndex:secondInputOption];
	

	
	[newGate  initWithInputs:[[NSArray alloc] initWithObjects:entry1, entry2, nil]];

	[entry1.outputGates addObject:newGate];
	[entry2.outputGates addObject:newGate];
	
	return newGate;
	
}

- (Gate *)specifySingleInputGate: (int)gateType {
	int inputOption;
	
	
	//Display the list of all avaiable outputs of each input
	for(int i = 0; i < [inputs count]; i++) {
		debug(@"%d. %@", i, [inputs objectAtIndex:i]);
	}
	debug(@"Choose the first input: ");	
	
	return nil;
}



@end
